package org.mikhaylov.cp.web.command;

import java.util.Map;
import java.util.TreeMap;

import org.apache.log4j.Logger;
import org.mikhaylov.cp.web.command.impl.LoginWebCommand;
import org.mikhaylov.cp.web.command.impl.LogoutWebCommand;
import org.mikhaylov.cp.web.command.impl.NoWebCommand;
import org.mikhaylov.cp.web.command.impl.RefillWebCommand;
import org.mikhaylov.cp.web.command.impl.RegisterWebCommand;
import org.mikhaylov.cp.web.command.impl.UpdateProfileWebCommand;
import org.mikhaylov.cp.web.command.redirect.MenuRedirectWebCommand;
import org.mikhaylov.cp.web.command.redirect.MyInvWebCommand;
import org.mikhaylov.cp.web.command.redirect.RefillRedirectWebCommand;
import org.mikhaylov.cp.web.command.redirect.RegistrationRedirectWebCommand;
import org.mikhaylov.cp.web.command.redirect.ViewProfileWebCommand;

/**
 * Holder for all commands.
 * 
 * @author Saint
 */
public class WebCommandContainer {

	private static final Logger log = Logger.getLogger(WebCommandContainer.class);

	private static Map<String, WebCommand> commands = new TreeMap<String, WebCommand>();

	static {
		// common commands
		commands.put("noCommand", new NoWebCommand());
		commands.put("logout", new LogoutWebCommand());

		// out of control commands
		commands.put("login", new LoginWebCommand());
		commands.put("registration", new RegistrationRedirectWebCommand());
		commands.put("register", new RegisterWebCommand());
		
		//commands.put("registration", new BrowseRegistrationCommand());
		//commands.put("register", new RegisterCommand());

		// client commands
		commands.put("menu", new MenuRedirectWebCommand());
		commands.put("refilling", new RefillRedirectWebCommand());
		commands.put("refill", new RefillWebCommand());
		commands.put("viewProfile", new ViewProfileWebCommand());
		commands.put("updateProfile", new UpdateProfileWebCommand());
		commands.put("myInventories", new MyInvWebCommand());
		
		// admin commands

		log.debug("Command container was successfully initialized");
		log.trace("Number of commands --> " + commands.size());
	}

	/**
	 * Returns command object with the given name.
	 * 
	 * @param commandName
	 *            Name of the command.
	 * @return Command object.
	 */
	public static WebCommand get(String commandName) {
		if (commandName == null || !commands.containsKey(commandName)) {
			log.trace("Command not found, name --> " + commandName);
			return commands.get("noCommand");
		}

		return commands.get(commandName);
	}

	/**
	 * Checks if the command exists. Use: filter only
	 * 
	 * @param commandName
	 *            Name of the command
	 * @return appropriate command name string
	 */
	public static String getCommandString(String commandName) {
		if (commandName == null || !commands.containsKey(commandName)) {
			log.trace("Command not found, name --> " + commandName);
			return "noCommand";
		}

		return commandName;
	}

}